package com.inei.evaluacionmype.controller.activity;

import java.util.ArrayList;
import java.util.List;
import com.inei.evaluacionmype.R;
import com.inei.evaluacionmype.controller.CapituloIIIController;
import com.inei.evaluacionmype.controller.JustificacionController;
import com.inei.evaluacionmype.model.entity.Cap03;
import com.inei.evaluacionmype.model.entity.Justificacion;
import com.inei.evaluacionmype.tools.Mensaje;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableRow;

public class CapituloIII_Activity extends Fragment {

	// componentes
	private View root;
	private TableRow trC301, trC302;
	private EditText etC3_01_01, etC3_01_02, etC3_01_03, etC3_01_04,
			etC3_02_01, etC3_02_02, etC3_02_03, etC3_02_04, etC3_03_01,
			etC3_03_02, etC3_03_03, etC3_03_04, etC3_04_01, etC3_04_02,
			etC3_04_03, etC3_04_04, etC3_05_01, etC3_05_02, etC3_05_03,
			etC3_05_04, etC3_06_01, etC3_06_02, etC3_06_03, etC3_06_04,
			etC3_07_01, etC3_07_02, etC3_07_03, etC3_07_04, etC3_08_01,
			etC3_08_02, etC3_08_03, etC3_08_04, etC3_09_01, etC3_09_02,
			etC3_09_03, etC301, etC302;

	// variables
	private CapituloIIIController con;
	private Cap03 obj;
	private JustificacionController con2;
	private List<Justificacion> lJust;
	private String ruc_id;
	private boolean nuevo = false;
	private boolean nuevoJust = false;
	private Mensaje mensaje;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.activity_capitulo_iii, container,
				false);

		return root;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		con = new CapituloIIIController(getActivity());
		con2 = new JustificacionController(getActivity());
		mensaje = new Mensaje(getActivity());

		IniciarComponentes();

		CargarDatos();

		AgregarEventos();
	}

	private void IniciarComponentes() {
		trC301 = (TableRow) root.findViewById(R.id.trC301);
		trC302 = (TableRow) root.findViewById(R.id.trC302);

		Ocultar();

		etC3_01_01 = (EditText) root.findViewById(R.id.etC3_01_01);
		etC3_01_02 = (EditText) root.findViewById(R.id.etC3_01_02);
		etC3_01_03 = (EditText) root.findViewById(R.id.etC3_01_03);
		etC3_01_04 = (EditText) root.findViewById(R.id.etC3_01_04);
		etC3_02_01 = (EditText) root.findViewById(R.id.etC3_02_01);
		etC3_02_02 = (EditText) root.findViewById(R.id.etC3_02_02);
		etC3_02_03 = (EditText) root.findViewById(R.id.etC3_02_03);
		etC3_02_04 = (EditText) root.findViewById(R.id.etC3_02_04);
		etC3_03_01 = (EditText) root.findViewById(R.id.etC3_03_01);
		etC3_03_02 = (EditText) root.findViewById(R.id.etC3_03_02);
		etC3_03_03 = (EditText) root.findViewById(R.id.etC3_03_03);
		etC3_03_04 = (EditText) root.findViewById(R.id.etC3_03_04);
		etC3_04_01 = (EditText) root.findViewById(R.id.etC3_04_01);
		etC3_04_02 = (EditText) root.findViewById(R.id.etC3_04_02);
		etC3_04_03 = (EditText) root.findViewById(R.id.etC3_04_03);
		etC3_04_04 = (EditText) root.findViewById(R.id.etC3_04_04);
		etC3_05_01 = (EditText) root.findViewById(R.id.etC3_05_01);
		etC3_05_02 = (EditText) root.findViewById(R.id.etC3_05_02);
		etC3_05_03 = (EditText) root.findViewById(R.id.etC3_05_03);
		etC3_05_04 = (EditText) root.findViewById(R.id.etC3_05_04);
		etC3_06_01 = (EditText) root.findViewById(R.id.etC3_06_01);
		etC3_06_02 = (EditText) root.findViewById(R.id.etC3_06_02);
		etC3_06_03 = (EditText) root.findViewById(R.id.etC3_06_03);
		etC3_06_04 = (EditText) root.findViewById(R.id.etC3_06_04);
		etC3_07_01 = (EditText) root.findViewById(R.id.etC3_07_01);
		etC3_07_02 = (EditText) root.findViewById(R.id.etC3_07_02);
		etC3_07_03 = (EditText) root.findViewById(R.id.etC3_07_03);
		etC3_07_04 = (EditText) root.findViewById(R.id.etC3_07_04);
		etC3_08_01 = (EditText) root.findViewById(R.id.etC3_08_01);
		etC3_08_02 = (EditText) root.findViewById(R.id.etC3_08_02);
		etC3_08_03 = (EditText) root.findViewById(R.id.etC3_08_03);
		etC3_08_04 = (EditText) root.findViewById(R.id.etC3_08_04);
		etC3_09_01 = (EditText) root.findViewById(R.id.etC3_09_01);
		etC3_09_02 = (EditText) root.findViewById(R.id.etC3_09_02);
		etC3_09_03 = (EditText) root.findViewById(R.id.etC3_09_03);
		etC301 = (EditText) root.findViewById(R.id.etC301);
		etC302 = (EditText) root.findViewById(R.id.etC302);
	}

	private void Ocultar() {
		trC301.setVisibility(View.GONE);
		trC302.setVisibility(View.GONE);
	}

	private void AgregarEventos() {
		EventoEditText(etC3_01_02);
		EventoEditText(etC3_01_03);
		EventoEditText(etC3_01_04);
		EventoEditText(etC3_03_02);
		EventoEditText(etC3_03_03);
		EventoEditText(etC3_03_04);
		EventoEditText(etC3_04_02);
		EventoEditText(etC3_04_03);
		EventoEditText(etC3_04_04);
		EventoEditText(etC3_06_02);
		EventoEditText(etC3_06_03);
		EventoEditText(etC3_06_04);
		EventoEditText(etC3_07_02);
		EventoEditText(etC3_07_03);
		EventoEditText(etC3_07_04);
		EventoEditText(etC3_09_02);
		EventoEditText(etC3_09_03);
	}

	private void EventoEditText(final EditText et) {
		et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				int n1_1 = 0;
				int n1_2 = 0;
				int n1_3 = 0;
				int n1_4 = 0;
				int n2_1 = 0;
				int n2_2 = 0;
				int n2_3 = 0;
				int n2_4 = 0;
				int n3_1 = Integer.parseInt(etC3_03_01.getText().toString()
						.trim().length() == 0 ? "0" : etC3_03_01.getText()
						.toString());
				int n3_2 = 0;
				int n3_3 = 0;
				int n3_4 = 0;
				int n4_1 = Integer.parseInt(etC3_04_01.getText().toString()
						.trim().length() == 0 ? "0" : etC3_04_01.getText()
						.toString());
				int n4_2 = 0;
				int n4_3 = 0;
				int n4_4 = 0;
				int n5_1 = 0;
				int n5_2 = 0;
				int n5_3 = 0;
				int n5_4 = 0;
				int n6_1 = 0;
				int n6_2 = 0;
				int n6_3 = 0;
				int n6_4 = 0;
				int n7_1 = Integer.parseInt(etC3_07_01.getText().toString()
						.trim().length() == 0 ? "0" : etC3_07_01.getText()
						.toString());
				int n7_2 = 0;
				int n7_3 = 0;
				int n7_4 = 0;
				int n8_1 = Integer.parseInt(etC3_08_01.getText().toString()
						.trim().length() == 0 ? "0" : etC3_08_01.getText()
						.toString());
				int n8_2 = Integer.parseInt(etC3_08_02.getText().toString()
						.trim().length() == 0 ? "0" : etC3_08_02.getText()
						.toString());
				int n8_3 = Integer.parseInt(etC3_08_03.getText().toString()
						.trim().length() == 0 ? "0" : etC3_08_03.getText()
						.toString());
				int n8_4 = Integer.parseInt(etC3_08_04.getText()
						.toString().trim().length() == 0 ? "0" : etC3_08_04
						.getText().toString());
				int n9_1 = Integer.parseInt(etC3_09_01.getText().toString()
						.trim().length() == 0 ? "0" : etC3_09_01.getText()
						.toString());
				int n9_2 = 0;
				int n9_3 = 0;

				switch (et.getId()) {
				case R.id.etC3_01_02:
					n1_2 = Integer.parseInt(etC3_01_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_02.getText()
							.toString());
					n1_3 = Integer.parseInt(etC3_01_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_03.getText()
							.toString());
					etC3_01_01.setText((n1_2 + n1_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n2_2 = Integer.parseInt(etC3_02_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_02.getText()
							.toString());
					n5_2 = Integer.parseInt(etC3_05_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_02.getText()
							.toString());
					etC3_08_02.setText((n1_2 + n2_2 + n5_2) + "");
					break;

				case R.id.etC3_01_03:
					n1_2 = Integer.parseInt(etC3_01_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_02.getText()
							.toString());
					n1_3 = Integer.parseInt(etC3_01_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_03.getText()
							.toString());
					etC3_01_01.setText((n1_2 + n1_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n2_3 = Integer.parseInt(etC3_02_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_03.getText()
							.toString());
					n5_3 = Integer.parseInt(etC3_05_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_03.getText()
							.toString());
					etC3_08_03.setText((n1_3 + n2_3 + n5_3) + "");
					break;

				case R.id.etC3_01_04:
					n1_4 = Integer.parseInt(etC3_01_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_04.getText()
							.toString());
					n2_4 = Integer.parseInt(etC3_02_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_04.getText()
							.toString());
					n5_4 = Integer.parseInt(etC3_05_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_04.getText()
							.toString());
					etC3_08_04.setText((n1_4 + n2_4 + n5_4) + "");
					break;

				case R.id.etC3_03_02:
					n3_2 = Integer.parseInt(etC3_03_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_03_02.getText()
							.toString());
					n4_2 = Integer.parseInt(etC3_04_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_04_02.getText()
							.toString());
					etC3_02_02.setText((n3_2 + n4_2) + "");
					n1_2 = Integer.parseInt(etC3_01_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_02.getText()
							.toString());
					n2_2 = Integer.parseInt(etC3_02_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_02.getText()
							.toString());
					n5_2 = Integer.parseInt(etC3_05_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_02.getText()
							.toString());
					etC3_08_02.setText((n1_2 + n2_2 + n5_2) + "");
					n2_3 = Integer.parseInt(etC3_02_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_03.getText()
							.toString());
					etC3_02_01.setText((n2_2 + n2_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n3_3 = Integer.parseInt(etC3_03_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_03_03.getText()
							.toString());
					etC3_03_01.setText((n3_2 + n3_3) + "");
					break;

				case R.id.etC3_03_03:
					n3_3 = Integer.parseInt(etC3_03_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_03_03.getText()
							.toString());
					n4_3 = Integer.parseInt(etC3_04_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_04_03.getText()
							.toString());
					etC3_02_03.setText((n3_3 + n4_3) + "");
					n1_3 = Integer.parseInt(etC3_01_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_03.getText()
							.toString());
					n2_3 = Integer.parseInt(etC3_02_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_03.getText()
							.toString());
					n5_3 = Integer.parseInt(etC3_05_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_03.getText()
							.toString());
					etC3_08_03.setText((n1_3 + n2_3 + n5_3) + "");
					n2_2 = Integer.parseInt(etC3_02_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_02.getText()
							.toString());
					etC3_02_01.setText((n2_2 + n2_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n3_2 = Integer.parseInt(etC3_03_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_03_02.getText()
							.toString());
					etC3_03_01.setText((n3_2 + n3_3) + "");
					break;

				case R.id.etC3_03_04:
					n3_4 = Integer.parseInt(etC3_03_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_03_04.getText()
							.toString());
					n4_4 = Integer.parseInt(etC3_04_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_04_04.getText()
							.toString());
					etC3_02_04.setText((n3_4 + n4_4) + "");
					n1_4 = Integer.parseInt(etC3_01_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_04.getText()
							.toString());
					n2_4 = Integer.parseInt(etC3_02_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_04.getText()
							.toString());
					n5_4 = Integer.parseInt(etC3_05_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_04.getText()
							.toString());
					etC3_08_04.setText((n1_4 + n2_4 + n5_4) + "");
					break;

				case R.id.etC3_04_02:
					n3_2 = Integer.parseInt(etC3_03_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_03_02.getText()
							.toString());
					n4_2 = Integer.parseInt(etC3_04_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_04_02.getText()
							.toString());
					etC3_02_02.setText((n3_2 + n4_2) + "");
					n1_2 = Integer.parseInt(etC3_01_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_02.getText()
							.toString());
					n2_2 = Integer.parseInt(etC3_02_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_02.getText()
							.toString());
					n5_2 = Integer.parseInt(etC3_05_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_02.getText()
							.toString());
					etC3_08_02.setText((n1_2 + n2_2 + n5_2) + "");
					n2_3 = Integer.parseInt(etC3_02_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_03.getText()
							.toString());
					etC3_02_01.setText((n2_2 + n2_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n4_3 = Integer.parseInt(etC3_04_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_04_03.getText()
							.toString());
					etC3_04_01.setText((n4_2 + n4_3) + "");
					break;

				case R.id.etC3_04_03:
					n3_3 = Integer.parseInt(etC3_03_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_03_03.getText()
							.toString());
					n4_3 = Integer.parseInt(etC3_04_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_04_03.getText()
							.toString());
					etC3_02_03.setText((n3_3 + n4_3) + "");
					n1_3 = Integer.parseInt(etC3_01_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_03.getText()
							.toString());
					n2_3 = Integer.parseInt(etC3_02_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_03.getText()
							.toString());
					n5_3 = Integer.parseInt(etC3_05_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_03.getText()
							.toString());
					etC3_08_03.setText((n1_3 + n2_3 + n5_3) + "");
					n2_2 = Integer.parseInt(etC3_02_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_02.getText()
							.toString());
					etC3_02_01.setText((n2_2 + n2_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n4_2 = Integer.parseInt(etC3_04_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_04_02.getText()
							.toString());
					etC3_04_01.setText((n4_2 + n4_3) + "");
					break;

				case R.id.etC3_04_04:
					n3_4 = Integer.parseInt(etC3_03_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_03_04.getText()
							.toString());
					n4_4 = Integer.parseInt(etC3_04_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_04_04.getText()
							.toString());
					etC3_02_04.setText((n3_4 + n4_4) + "");
					n1_4 = Integer.parseInt(etC3_01_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_04.getText()
							.toString());
					n2_4 = Integer.parseInt(etC3_02_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_04.getText()
							.toString());
					n5_4 = Integer.parseInt(etC3_05_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_04.getText()
							.toString());
					etC3_08_04.setText((n1_4 + n2_4 + n5_4) + "");
					break;

				case R.id.etC3_06_02:
					n6_2 = Integer.parseInt(etC3_06_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_06_02.getText()
							.toString());
					n7_2 = Integer.parseInt(etC3_07_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_07_02.getText()
							.toString());
					etC3_05_02.setText((n6_2 + n7_2) + "");
					n1_2 = Integer.parseInt(etC3_01_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_02.getText()
							.toString());
					n2_2 = Integer.parseInt(etC3_02_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_02.getText()
							.toString());
					n5_2 = Integer.parseInt(etC3_05_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_02.getText()
							.toString());
					etC3_08_02.setText((n1_2 + n2_2 + n5_2) + "");
					n5_3 = Integer.parseInt(etC3_05_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_03.getText()
							.toString());
					etC3_05_01.setText((n5_2 + n5_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n6_3 = Integer.parseInt(etC3_06_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_06_03.getText()
							.toString());
					etC3_06_01.setText((n6_2 + n6_3) + "");
					break;

				case R.id.etC3_06_03:
					n6_3 = Integer.parseInt(etC3_06_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_06_03.getText()
							.toString());
					n7_3 = Integer.parseInt(etC3_07_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_07_03.getText()
							.toString());
					etC3_05_03.setText((n6_3 + n7_3) + "");
					n1_3 = Integer.parseInt(etC3_01_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_03.getText()
							.toString());
					n2_3 = Integer.parseInt(etC3_02_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_03.getText()
							.toString());
					n5_3 = Integer.parseInt(etC3_05_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_03.getText()
							.toString());
					etC3_08_03.setText((n1_3 + n2_3 + n5_3) + "");
					n5_2 = Integer.parseInt(etC3_05_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_02.getText()
							.toString());
					etC3_05_01.setText((n5_2 + n5_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n6_2 = Integer.parseInt(etC3_06_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_06_02.getText()
							.toString());
					etC3_06_01.setText((n6_2 + n6_3) + "");
					break;

				case R.id.etC3_06_04:
					n6_4 = Integer.parseInt(etC3_06_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_06_04.getText()
							.toString());
					n7_4 = Integer.parseInt(etC3_07_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_07_04.getText()
							.toString());
					etC3_05_04.setText((n6_4 + n7_4) + "");
					n1_4 = Integer.parseInt(etC3_01_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_04.getText()
							.toString());
					n2_4 = Integer.parseInt(etC3_02_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_04.getText()
							.toString());
					n5_4 = Integer.parseInt(etC3_05_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_04.getText()
							.toString());
					etC3_08_04.setText((n1_4 + n2_4 + n5_4) + "");
					break;

				case R.id.etC3_07_02:
					n6_2 = Integer.parseInt(etC3_06_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_06_02.getText()
							.toString());
					n7_2 = Integer.parseInt(etC3_07_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_07_02.getText()
							.toString());
					etC3_05_02.setText((n6_2 + n7_2) + "");
					n1_2 = Integer.parseInt(etC3_01_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_02.getText()
							.toString());
					n2_2 = Integer.parseInt(etC3_02_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_02.getText()
							.toString());
					n5_2 = Integer.parseInt(etC3_05_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_02.getText()
							.toString());
					etC3_08_02.setText((n1_2 + n2_2 + n5_2) + "");
					n5_3 = Integer.parseInt(etC3_05_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_03.getText()
							.toString());
					etC3_05_01.setText((n5_2 + n5_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n7_3 = Integer.parseInt(etC3_07_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_07_03.getText()
							.toString());
					etC3_07_01.setText((n7_2 + n7_3) + "");
					break;

				case R.id.etC3_07_03:
					n6_3 = Integer.parseInt(etC3_06_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_06_03.getText()
							.toString());
					n7_3 = Integer.parseInt(etC3_07_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_07_03.getText()
							.toString());
					etC3_05_03.setText((n6_3 + n7_3) + "");
					n1_3 = Integer.parseInt(etC3_01_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_03.getText()
							.toString());
					n2_3 = Integer.parseInt(etC3_02_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_03.getText()
							.toString());
					n5_3 = Integer.parseInt(etC3_05_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_03.getText()
							.toString());
					etC3_08_03.setText((n1_3 + n2_3 + n5_3) + "");
					n5_2 = Integer.parseInt(etC3_05_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_02.getText()
							.toString());
					etC3_05_01.setText((n5_2 + n5_3) + "");
					n1_1 = Integer.parseInt(etC3_01_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_01.getText()
							.toString());
					n2_1 = Integer.parseInt(etC3_02_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_01.getText()
							.toString());
					n5_1 = Integer.parseInt(etC3_05_01.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_01.getText()
							.toString());
					etC3_08_01.setText((n1_1 + n2_1 + n5_1) + "");
					n7_2 = Integer.parseInt(etC3_07_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_07_02.getText()
							.toString());
					etC3_07_01.setText((n7_2 + n7_3) + "");
					break;

				case R.id.etC3_07_04:
					n6_4 = Integer.parseInt(etC3_06_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_06_04.getText()
							.toString());
					n7_4 = Integer.parseInt(etC3_07_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_07_04.getText()
							.toString());
					etC3_05_04.setText((n6_4 + n7_4) + "");
					n1_4 = Integer.parseInt(etC3_01_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_01_04.getText()
							.toString());
					n2_4 = Integer.parseInt(etC3_02_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_02_04.getText()
							.toString());
					n5_4 = Integer.parseInt(etC3_05_04.getText().toString()
							.trim().length() == 0 ? "0" : etC3_05_04.getText()
							.toString());
					etC3_08_04.setText((n1_4 + n2_4 + n5_4) + "");
					break;
				case R.id.etC3_09_02:
					n9_2 = Integer.parseInt(etC3_09_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_09_02.getText()
							.toString());
					n9_3 = Integer.parseInt(etC3_09_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_09_03.getText()
							.toString());
					etC3_09_01.setText((n9_2 + n9_3) + "");
					break;

				case R.id.etC3_09_03:
					n9_2 = Integer.parseInt(etC3_09_02.getText().toString()
							.trim().length() == 0 ? "0" : etC3_09_02.getText()
							.toString());
					n9_3 = Integer.parseInt(etC3_09_03.getText().toString()
							.trim().length() == 0 ? "0" : etC3_09_03.getText()
							.toString());
					etC3_09_01.setText((n9_2 + n9_3) + "");
					break;

				default:
					break;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	private void CargarDatos() {
		ruc_id = getArguments().getString("ruc_id");
		obj = new Cap03();
		obj.setRUC(ruc_id);

		obj = con.Cap03Obtener(obj);
		if (obj != null) {
			etC3_01_01.setText(obj.getC3P1_01_TOTAL() + "");
			etC3_01_02.setText(obj.getC3P1_02_HOMBRES() + "");
			etC3_01_03.setText(obj.getC3P1_03_MUJERES() + "");
			etC3_01_04.setText(obj.getC3P1_04_TOTGASTOS() + "");
			etC3_02_01.setText(obj.getC3P2_01_TOTAL() + "");
			etC3_02_02.setText(obj.getC3P2_02_HOMBRES() + "");
			etC3_02_03.setText(obj.getC3P2_03_MUJERES() + "");
			etC3_02_04.setText(obj.getC3P2_04_TOTGASTOS() + "");
			etC3_03_01.setText(obj.getC3P2A_01_TOTAL() + "");
			etC3_03_02.setText(obj.getC3P2A_02_HOMBRES() + "");
			etC3_03_03.setText(obj.getC3P2A_03_MUJERES() + "");
			etC3_03_04.setText(obj.getC3P2A_04_TOTGASTOS() + "");
			etC3_04_01.setText(obj.getC3P2B_01_TOTAL() + "");
			etC3_04_02.setText(obj.getC3P2B_02_HOMBRES() + "");
			etC3_04_03.setText(obj.getC3P2B_03_MUJERES() + "");
			etC3_04_04.setText(obj.getC3P2B_04_TOTGASTOS() + "");
			etC3_05_01.setText(obj.getC3P3_01_TOTAL() + "");
			etC3_05_02.setText(obj.getC3P3_02_HOMBRES() + "");
			etC3_05_03.setText(obj.getC3P3_03_MUJERES() + "");
			etC3_05_04.setText(obj.getC3P3_04_TOTGASTOS() + "");
			etC3_06_01.setText(obj.getC3P3A_01_TOTAL() + "");
			etC3_06_02.setText(obj.getC3P3A_02_HOMBRES() + "");
			etC3_06_03.setText(obj.getC3P3A_03_MUJERES() + "");
			etC3_06_04.setText(obj.getC3P3A_04_TOTGASTOS() + "");
			etC3_07_01.setText(obj.getC3P3B_01_TOTAL() + "");
			etC3_07_02.setText(obj.getC3P3B_02_HOMBRES() + "");
			etC3_07_03.setText(obj.getC3P3B_03_MUJERES() + "");
			etC3_07_04.setText(obj.getC3P3B_04_TOTGASTOS() + "");
			etC3_08_01.setText(obj.getC3TOTAL_01_TOTAL() + "");
			etC3_08_02.setText(obj.getC3TOTAL_02_HOMBRES() + "");
			etC3_08_03.setText(obj.getC3TOTAL_03_MUJERES() + "");
			etC3_08_04.setText(obj.getC3TOTAL_04_TOTGASTOS() + "");
			etC3_09_01.setText(obj.getC3P4_01_TOTAL() + "");
			etC3_09_02.setText(obj.getC3P4_02_HOMBRES() + "");
			etC3_09_03.setText(obj.getC3P4_03_MUJERES() + "");
		}

		ValidarCap();

		lJust = con2.JustificacionObtenerPorRuc(ruc_id, "3");
		if (lJust == null) {
			nuevoJust = true;
			lJust = new ArrayList<Justificacion>();
			for (int i = 0; i < 2; i++) {
				Justificacion just = new Justificacion();
				just.setRUC(ruc_id);
				just.setCAPITULO("3");
				just.setCLAVE(i + "");
				just.setFLAG("1");
				lJust.add(i, just);
			}
			lJust.get(0).setREGLA(
					"MP_03O01: Cap 03, col 01, claves 08 + 09 > 0");
			lJust.get(1).setREGLA("MP_03O02: Cap 03, col 04, clave 08 > 0");
		} else {
			if (lJust.get(0).getDETALLE() != null) {
				etC301.setText(lJust.get(0).getDETALLE());
			}
			if (lJust.get(1).getDETALLE() != null) {
				etC302.setText(lJust.get(1).getDETALLE());
			}
		}
	}

	public void ValidarCap() {
		int c08_01 = Integer.parseInt(etC3_08_01.getText().toString().trim()
				.length() == 0 ? "0" : etC3_08_01.getText().toString());
		int c09_01 = Integer.parseInt(etC3_09_01.getText().toString().trim()
				.length() == 0 ? "0" : etC3_09_01.getText().toString());

		if (c08_01 + c09_01 > 0) {
			trC301.setVisibility(View.VISIBLE);
		} else {
			etC301.setText("");
			trC301.setVisibility(View.GONE);
		}

		int c08_04 = Integer.parseInt(etC3_08_04.getText().toString().trim()
				.length() == 0 ? "0" : etC3_08_04.getText().toString());

		if (c08_04 > 0) {
			trC302.setVisibility(View.VISIBLE);
		} else {
			etC302.setText("");
			trC302.setVisibility(View.GONE);
		}
	}

	public void GuardarCap() {
		if (obj == null) {
			nuevo = true;
			obj = new Cap03();
			obj.setRUC(ruc_id);
		}

		obj.setC3P1_01_TOTAL(Integer.parseInt(etC3_01_01.getText().toString()
				.trim().length() == 0 ? "0" : etC3_01_01.getText().toString()));
		obj.setC3P1_02_HOMBRES(Integer.parseInt(etC3_01_02.getText().toString()
				.trim().length() == 0 ? "0" : etC3_01_02.getText().toString()));
		obj.setC3P1_03_MUJERES(Integer.parseInt(etC3_01_03.getText().toString()
				.trim().length() == 0 ? "0" : etC3_01_03.getText().toString()));
		obj.setC3P1_04_TOTGASTOS(Integer.parseInt(etC3_01_04.getText()
				.toString().trim().length() == 0 ? "0" : etC3_01_04.getText()
				.toString()));
		obj.setC3P2_01_TOTAL(Integer.parseInt(etC3_02_01.getText().toString()
				.trim().length() == 0 ? "0" : etC3_02_01.getText().toString()));
		obj.setC3P2_02_HOMBRES(Integer.parseInt(etC3_02_02.getText().toString()
				.trim().length() == 0 ? "0" : etC3_02_02.getText().toString()));
		obj.setC3P2_03_MUJERES(Integer.parseInt(etC3_02_03.getText().toString()
				.trim().length() == 0 ? "0" : etC3_02_03.getText().toString()));
		obj.setC3P2_04_TOTGASTOS(Integer.parseInt(etC3_02_04.getText()
				.toString().trim().length() == 0 ? "0" : etC3_02_04.getText()
				.toString()));
		obj.setC3P2A_01_TOTAL(Integer.parseInt(etC3_03_01.getText().toString()
				.trim().length() == 0 ? "0" : etC3_03_01.getText().toString()));
		obj.setC3P2A_02_HOMBRES(Integer.parseInt(etC3_03_02.getText()
				.toString().trim().length() == 0 ? "0" : etC3_03_02.getText()
				.toString()));
		obj.setC3P2A_03_MUJERES(Integer.parseInt(etC3_03_03.getText()
				.toString().trim().length() == 0 ? "0" : etC3_03_03.getText()
				.toString()));
		obj.setC3P2A_04_TOTGASTOS(Integer.parseInt(etC3_03_04.getText()
				.toString().trim().length() == 0 ? "0" : etC3_03_04.getText()
				.toString()));
		obj.setC3P2B_01_TOTAL(Integer.parseInt(etC3_04_01.getText().toString()
				.trim().length() == 0 ? "0" : etC3_04_01.getText().toString()));
		obj.setC3P2B_02_HOMBRES(Integer.parseInt(etC3_04_02.getText()
				.toString().trim().length() == 0 ? "0" : etC3_04_02.getText()
				.toString()));
		obj.setC3P2B_03_MUJERES(Integer.parseInt(etC3_04_03.getText()
				.toString().trim().length() == 0 ? "0" : etC3_04_03.getText()
				.toString()));
		obj.setC3P2B_04_TOTGASTOS(Integer.parseInt(etC3_04_04.getText()
				.toString().trim().length() == 0 ? "0" : etC3_04_04.getText()
				.toString()));
		obj.setC3P3_01_TOTAL(Integer.parseInt(etC3_05_01.getText().toString()
				.trim().length() == 0 ? "0" : etC3_05_01.getText().toString()));
		obj.setC3P3_02_HOMBRES(Integer.parseInt(etC3_05_02.getText().toString()
				.trim().length() == 0 ? "0" : etC3_05_02.getText().toString()));
		obj.setC3P3_03_MUJERES(Integer.parseInt(etC3_05_03.getText().toString()
				.trim().length() == 0 ? "0" : etC3_05_03.getText().toString()));
		obj.setC3P3_04_TOTGASTOS(Integer.parseInt(etC3_05_04.getText()
				.toString().trim().length() == 0 ? "0" : etC3_05_04.getText()
				.toString()));
		obj.setC3P3A_01_TOTAL(Integer.parseInt(etC3_06_01.getText().toString()
				.trim().length() == 0 ? "0" : etC3_06_01.getText().toString()));
		obj.setC3P3A_02_HOMBRES(Integer.parseInt(etC3_06_02.getText()
				.toString().trim().length() == 0 ? "0" : etC3_06_02.getText()
				.toString()));
		obj.setC3P3A_03_MUJERES(Integer.parseInt(etC3_06_03.getText()
				.toString().trim().length() == 0 ? "0" : etC3_06_03.getText()
				.toString()));
		obj.setC3P3A_04_TOTGASTOS(Integer.parseInt(etC3_06_04.getText()
				.toString().trim().length() == 0 ? "0" : etC3_06_04.getText()
				.toString()));
		obj.setC3P3B_01_TOTAL(Integer.parseInt(etC3_07_01.getText().toString()
				.trim().length() == 0 ? "0" : etC3_07_01.getText().toString()));
		obj.setC3P3B_02_HOMBRES(Integer.parseInt(etC3_07_02.getText()
				.toString().trim().length() == 0 ? "0" : etC3_07_02.getText()
				.toString()));
		obj.setC3P3B_03_MUJERES(Integer.parseInt(etC3_07_03.getText()
				.toString().trim().length() == 0 ? "0" : etC3_07_03.getText()
				.toString()));
		obj.setC3P3B_04_TOTGASTOS(Integer.parseInt(etC3_07_04.getText()
				.toString().trim().length() == 0 ? "0" : etC3_07_04.getText()
				.toString()));
		obj.setC3TOTAL_01_TOTAL(Integer.parseInt(etC3_08_01.getText()
				.toString().trim().length() == 0 ? "0" : etC3_08_01.getText()
				.toString()));
		obj.setC3TOTAL_02_HOMBRES(Integer.parseInt(etC3_08_02.getText()
				.toString().trim().length() == 0 ? "0" : etC3_08_02.getText()
				.toString()));
		obj.setC3TOTAL_03_MUJERES(Integer.parseInt(etC3_08_03.getText()
				.toString().trim().length() == 0 ? "0" : etC3_08_03.getText()
				.toString()));
		obj.setC3TOTAL_04_TOTGASTOS(Integer.parseInt(etC3_08_04.getText()
				.toString().trim().length() == 0 ? "0" : etC3_08_04.getText()
				.toString()));
		obj.setC3P4_01_TOTAL(Integer.parseInt(etC3_09_01.getText().toString()
				.trim().length() == 0 ? "0" : etC3_09_01.getText().toString()));
		obj.setC3P4_02_HOMBRES(Integer.parseInt(etC3_09_02.getText().toString()
				.trim().length() == 0 ? "0" : etC3_09_02.getText().toString()));
		obj.setC3P4_03_MUJERES(Integer.parseInt(etC3_09_03.getText().toString()
				.trim().length() == 0 ? "0" : etC3_09_03.getText().toString()));

		if (nuevo) {
			if (con.Cap03Insertar(obj) > 0) {
				nuevo = false;
				ValidarJust();
				mensaje.Exito("SE GRABO CON EXITO");
			} else {
				mensaje.Error("ERROR AL GRABAR");
			}
		} else {
			if (con.Cap03Modificar(obj) > 0) {
				ValidarJust();
				mensaje.Exito("SE MODIFICO CON EXITO");
			} else {
				mensaje.Error("ERROR AL MODIFICAR");
			}
		}
	}

	private void ValidarJust() {
		int c08_01 = Integer.parseInt(etC3_08_01.getText().toString().trim()
				.length() == 0 ? "0" : etC3_08_01.getText().toString());
		int c09_01 = Integer.parseInt(etC3_09_01.getText().toString().trim()
				.length() == 0 ? "0" : etC3_09_01.getText().toString());

		if ((c08_01 + c09_01) > 0
				&& etC301.getText().toString().trim().length() == 0) {
			lJust.get(0).setDETALLE(etC301.getText().toString());
			lJust.get(0).setFLAG("0");
		} else {
			lJust.get(0).setDETALLE(etC301.getText().toString());
			lJust.get(0).setFLAG("1");
		}

		int c08_04 = Integer.parseInt(etC3_08_04.getText().toString().trim()
				.length() == 0 ? "0" : etC3_08_04.getText().toString());

		if (c08_04 > 0 && etC302.getText().toString().trim().length() == 0) {
			lJust.get(1).setDETALLE(etC302.getText().toString());
			lJust.get(1).setFLAG("0");
		} else {
			lJust.get(1).setDETALLE(etC302.getText().toString());
			lJust.get(1).setFLAG("1");
		}

		if (nuevoJust) {
			for (int i = 0; i < 2; i++) {
				con2.JustificacionInsertar(lJust.get(i));
			}
			nuevoJust = false;
		} else {
			for (int i = 0; i < 2; i++) {
				con2.JustificacionModificar(lJust.get(i));
			}
		}
	}
}
